package edu.ufrj.pesc.filecrawler;

import java.io.File;     // for File
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.*;   // for Scanner
import org.apache.tika.Tika;

import edu.ufrj.pesc.softwaregraph.*;

public class FileCrawler {
	
	//private static Vector<String> stopList;
	private static Vector<SoftwareGraph> fileSystemTree;
	
	public static void main(String[] args) {
		SoftwareGraph sg;
		
		Scanner console = new Scanner(System.in);
        System.out.print("Directory to crawl? ");
        String fs = console.nextLine();
		
		
        fileSystemTree = new Vector<SoftwareGraph>();
        //for (Path fs: FileSystems.getDefault().getRootDirectories()) {
        	sg = new SoftwareGraph();
        	fileSystemTree.add(sg);
        	System.out.println("Crawling: "+fs+" file system;");
        	File f = new File(fs);
            crawl(f, sg);
            System.out.println(fileSystemTree.lastElement().toString());
            console.nextLine();
        //}
        
        console.close();
    }
    
    // Prints the given file/directory and any files/directories inside it,
    // starting with zero indentation.
    // Precondition: f != null and f exists
    public static void crawl(File f, SoftwareGraph sg) {
    	SoftwareGraphNode sg_node = new SoftwareGraphNode(f.getName(), f.getAbsolutePath());
    	sg.addNode(sg_node);
    	if (f.isDirectory()) {
        	// recursive case: directory
        	// print everything in the directory
    		sg_node.setType("Directory");
        	File[] subFiles = f.listFiles();
    		//indent += "    ";
        	for (int i = 0; i < subFiles.length; i++) {
              	crawl(subFiles[i], sg, sg_node);
        	}
        }
    	else
    		classifyLeaf(sg_node);
    }
    
    // This recursive "helper" method prints the given file/directory and any
    // files/directories inside it at the given level of indentation.
    // Precondition: f != null and f exists
    private static void crawl(File f, SoftwareGraph sg, SoftwareGraphNode parent) {
    	SoftwareGraphNode sg_node = new SoftwareGraphNode(f.getName(), f.getAbsolutePath());
    	sg.addEdge(parent,sg_node);
    	sg.addNode(sg_node);
    	//System.out.println(indent + f.getName() + " - " + f.getAbsolutePath());
    	if (f.isDirectory()) {
        	// recursive case: directory
        	// print everything in the directory
    		sg_node.setType("Directory");
        	File[] subFiles = f.listFiles();
    		//indent += "    ";
        	for (int i = 0; i < subFiles.length; i++) {
              	crawl(subFiles[i], sg, sg_node);
        	}
        }
    	else
    		classifyLeaf(sg_node);
    }
    
    private static void classifyLeaf(SoftwareGraphNode sg_node) {
    	Tika tika = new Tika();
    	String filename = sg_node.getPath();

    	String type;
		try {
			type = tika.detect(new File(filename));
	   		sg_node.setType(type);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("IO Exception");
			e.printStackTrace();
		}
    }
    
}
